
//
// 120401 - AH - Created.
// 120727 - AH - (Midtun) Now extends ParametersSerializable and removed code
//               that now is in base class.
// 120728 - AH - EMPTY_ARRAY.
// 120825 - AH - Minor java doc fix.
//

package org.soekrat.server.serialization;

import java.util.Map;

import org.soekrat.server.io.db.Folder;

/**
 * <p>
 *   Describes a
 *   {@link Folder}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class FolderSerializable extends ParametersSerializable 
{
  private static final long serialVersionUID=-1841937295896959884L;

  private Long id;
  
  private String name;
  
  private Long owner;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param folder Object restored from database {@code this} is to represents.
   */
  public FolderSerializable(Folder folder)
  {
    if (folder==null)
    {
      throw new NullPointerException("folder");
    }
    
    id=folder.id();
    name=folder.name();
    owner=folder.ownerId();
    
    Map<String, String> params=folder.getParameterMap();
    for (Map.Entry<String, String> curr : params.entrySet())
    {
      parameter(curr.getKey(), curr.getValue());  
    }
    
    if (id!=null)    parameter("id", Long.toString(id));
    if (owner!=null) parameter("owner", Long.toString(owner));
    if (name!=null)  parameter("name", name);
  }
  
  /**
   * <p>
   *   Gets id that identifies folder {@code this} represents.
   * </p>
   * @return Id.
   */
  public long id(){ return id; }
  
  /**
   * <p>
   *   Gets id of owner (folder id).
   * </p>
   * @return Owner's id or {@code null} if not sat. 
   */
  public Long owner(){ return this.owner; }
  
  /**
   * <p>
   *   Sets id that identifies owner of object {@code this} represents.
   * </p>
   * @param id Id to set.
   * @return {@code this}.
   * @throws IllegalArgumentException If {@code id<0}.
   */
  public FolderSerializable owner(long id)
  {
    if (id<0)
    {
      throw new IllegalArgumentException("id<0 : "+id);
    }
    
    this.id=id;
    
    return this;
  }
  
  /**
   * <p>
   *   Sets name of folder.
   * </p>
   * @param name Name.
   * @return {@code this}.
   */
  public FolderSerializable name(String name)
  { 
    this.name=name; 
    return this; 
  }
  
  /**
   * <p>
   *   Gets name of conversation.
   * </p>
   * @return Name or {@code null}.
   */
  public String name(){ return name; }
  
  public static final FolderSerializable[] EMPTY_ARRAY=
    new FolderSerializable[0];
  
}
